import axios from "axios";
import { ElMessage } from "element-plus";

const request = axios.create({
 baseURL: "https://www.cnmall.io",
 // baseURL: "https://www.amazonshop.io",
 timeout: 120000,
  headers: {
    "Content-Type": "application/json; charset=UTF-8",
  },
});
request.interceptors.request.use((config) => {
  if (localStorage.getItem("token")) {
    config.headers["Authorization"] =
      "Bearer " + sessionStorage.getItem("token");
  }

  return config;
});
request.interceptors.response.use((response) => {
  if (response.data.code == 200) {
    return response;
  } else {
    alert(response.data.msg);
  }
});
export const formatUrl = (url: string) => {
  if (!url || url == "") return undefined;
  if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1)
    return "https://www.amazonshop.io" + url;
  return url;
};
export default request;
